package com.znyj.servlet.thrift;

import com.alibaba.fastjson.JSONObject;
import com.znyj.servlet.constant.SysConstant;
import com.znyj.servlet.thrift.lower.*;
import org.apache.log4j.Logger;
import org.apache.thrift.TException;
import org.apache.thrift.protocol.TBinaryProtocol;
import org.apache.thrift.protocol.TProtocol;
import org.apache.thrift.transport.TFramedTransport;
import org.apache.thrift.transport.TSocket;
import org.apache.thrift.transport.TTransport;
import org.apache.thrift.transport.TTransportException;

/**
 * Created by gaoliwei02 on 17/8/9.
 */
public class ThriftClient {
    private static final Logger logger = Logger.getLogger(ThriftClient.class);

    public static JSONObject taskSendThriftApi(
            String userId,
            String deviceId,
            long createTime,
            long durationSec,
            String controlMsg,
            String userLocation) {

        TTransport transport = null;
        JSONObject jsonObject = new JSONObject();
        try {
            transport = new TFramedTransport(new TSocket(SysConstant.THRIFT_ADDR, SysConstant.THRIFT_PORT, SysConstant.THRIFT_TIMEOUT));
            TProtocol protocol = new TBinaryProtocol(transport);
            InnerThriftService.Client client = new InnerThriftService.Client(protocol);
            transport.open();

            RequestTaskMsg requestTaskMsg = new RequestTaskMsg();
            requestTaskMsg.setUserId(userId);
            requestTaskMsg.setDeviceId(deviceId);
            requestTaskMsg.setCreateTime(createTime);
            requestTaskMsg.setDurationSec(durationSec);
            requestTaskMsg.setControlMsg(controlMsg);
            requestTaskMsg.setUserLocation(userLocation);
            ResponseTaskMsg responseTaskMsg = client.sendTask2backEnd(requestTaskMsg);

            jsonObject.put("deviceId", responseTaskMsg.getDeviceId());
            jsonObject.put("orderId", responseTaskMsg.getOrderId());
            jsonObject.put("userId", responseTaskMsg.getUserId());
            jsonObject.put("status", responseTaskMsg.getErrorMsg());

        } catch (TTransportException e) {
            e.printStackTrace();
        } catch (TException e) {
            e.printStackTrace();
        } finally {
            if (null != transport) {
                transport.close();
            }
            return jsonObject;
        }
    }

    public static JSONObject taskResendThriftApi(
            long orderId,
            String deviceId,
            long createTime,
            String controlMsg) {

        TTransport transport = null;
        JSONObject jsonObject = new JSONObject();
        try {
            transport = new TFramedTransport(new TSocket(SysConstant.THRIFT_ADDR, SysConstant.THRIFT_PORT, SysConstant.THRIFT_TIMEOUT));
            TProtocol protocol = new TBinaryProtocol(transport);
            InnerThriftService.Client client = new InnerThriftService.Client(protocol);
            transport.open();

            ResponseTaskMsg responseTaskMsg = client.resendTask2backEnd(orderId, deviceId, createTime, controlMsg);

            jsonObject.put("deviceId", responseTaskMsg.getDeviceId());
            jsonObject.put("orderId", responseTaskMsg.getOrderId());
            jsonObject.put("userId", responseTaskMsg.getUserId());
            jsonObject.put("status", responseTaskMsg.getErrorMsg());

        } catch (TTransportException e) {
            e.printStackTrace();
        } catch (TException e) {
            e.printStackTrace();
        } finally {
            if (null != transport) {
                transport.close();
            }
            return jsonObject;
        }
    }

    public static JSONObject updateSendThriftApi(String userId, String deviceId, String upMsg) {
        TTransport transport = null;
        JSONObject jsonObject = new JSONObject();
        try {
            transport = new TFramedTransport(new TSocket(SysConstant.THRIFT_ADDR, SysConstant.THRIFT_PORT, SysConstant.THRIFT_TIMEOUT));
            TProtocol protocol = new TBinaryProtocol(transport);
            InnerThriftService.Client client = new InnerThriftService.Client(protocol);
            transport.open();

            RequestUpdateMsg requestUpdateMsg = new RequestUpdateMsg();
            requestUpdateMsg.setUserId(userId);
            requestUpdateMsg.setDeviceId(deviceId);
            requestUpdateMsg.setUpMsg(upMsg);

            ResponseUpdateMsg responseUpdateMsg = client.sendUpdate2backEnd(requestUpdateMsg);
            jsonObject.put("deviceId", responseUpdateMsg.getDeviceId());
            jsonObject.put("orderId", responseUpdateMsg.getOrderId());
            jsonObject.put("userId", responseUpdateMsg.getUserId());
            jsonObject.put("status", responseUpdateMsg.getErrorMsg());
        } catch (TTransportException e) {
            e.printStackTrace();
        } catch (TException e) {
            e.printStackTrace();
        } finally {
            if (null != transport) {
                transport.close();
            }
            return jsonObject;
        }
    }


    public static String getRealTimeDeviceInfo(String deviceId) {
        TTransport transport = null;
        String result = null;
        try {
            transport = new TFramedTransport(new TSocket(SysConstant.THRIFT_ADDR, SysConstant.THRIFT_PORT, SysConstant.THRIFT_TIMEOUT));
            TProtocol protocol = new TBinaryProtocol(transport);
            InnerThriftService.Client client = new InnerThriftService.Client(protocol);
            transport.open();
            result = client.getRealTimeDeviceInfo(deviceId);
        } catch (TTransportException e) {
            e.printStackTrace();
        } catch (TException e) {
            e.printStackTrace();
        } finally {
            if (null != transport) {
                transport.close();
            }
            return result;
        }
    }
//
//    public static String getSystemInfo(String res) {
//        TTransport transport = null;
//        String result = null;
//        try {
//            transport = new TFramedTransport(new TSocket(SysConstant.THRIFT_ADDR, SysConstant.THRIFT_PORT, SysConstant.THRIFT_TIMEOUT));
//            TProtocol protocol = new TCompactProtocol(transport);
//            InnerThriftService.Client client = new InnerThriftService.Client(protocol);
//            transport.open();
//            result = client.getSystemInfo(res);
//        } catch (TTransportException e) {
//            e.printStackTrace();
//        } catch (TException e) {
//            e.printStackTrace();
//        } finally {
//            if (null != transport) {
//                transport.close();
//            }
//            return result;
//        }
//    }
}
